/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataWanderMainFrame.java
 *
 * Created on 2010-03-11, 23:50:42
 */

package com.googlecode.datawander.View;

import com.googlecode.datawander.Settings.Preferences;
import com.googlecode.datawander.Settings.PreferencesException;
import com.googlecode.datawander.Presenter.DataWanderPresenter;
import com.googlecode.datawander.Presenter.DataWanderViewInt;
import com.googlecode.datawander.i18n.I18n;
import com.googlecode.datawander.i18n.I18n.LANGUAGE;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Marcin Stachniuk
 */
public class DataWanderMainFrame extends javax.swing.JFrame
        implements DataWanderViewInt {

    private DataWanderPresenter dataWanderControler;
    private PreferenceJFrame preferenceJFrame;
    private static final Logger logger = Logger.getLogger(DataWanderMainFrame.class);

    /** Creates new form DataWanderMainFrame */
    public DataWanderMainFrame() {
        // start configuration
        configureLog4j();
        logger.info("Log4j configured");

        try {
            Preferences pref = new Preferences();
            pref.load();
            I18n.setLanguage(pref.getLang());
            logger.info(I18n.getString("choseLanguage"));
        } catch (Exception e) {
            logger.fatal("I can't choose default language", e);
            return;
        }

        
        logger.trace("DataWanderMainFrame()");

        dataWanderControler = new DataWanderPresenter();
        dataWanderControler.setDataWanderView(this);
        
        initComponents();

        dataWanderMainPanel.setDataWanderControler(dataWanderControler);
    }

    public static void configureLog4j() {
        try {
            BasicConfigurator.configure();

            String userDir = System.getProperty("user.dir");
            String fileSeparator = System.getProperty("file.separator");

            String path = userDir+fileSeparator+"log4j.properties";
            Properties logProperties = new Properties();
            logProperties.load(new FileInputStream(path));
            PropertyConfigurator.configure(logProperties);
        } catch(IOException e) {
            e.printStackTrace();
            logger.fatal(e.toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        dataWanderMainPanel = new com.googlecode.datawander.View.DataWanderMainPanel();
        jMenuBarMain = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuEdit = new javax.swing.JMenu();
        jMenuItemPreferences = new javax.swing.JMenuItem();
        jMenuMigration = new javax.swing.JMenu();
        jMenuItemRelationalToObject = new javax.swing.JMenuItem();
        jMenuItemObjectToRelational = new javax.swing.JMenuItem();
        jMenuHelp = new javax.swing.JMenu();
        jMenuItemHelp = new javax.swing.JMenuItem();
        jMenuItemAboutProgram = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DataWander by Marcin Stachniuk");
        setMinimumSize(new java.awt.Dimension(850, 350));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                DataWanderMainFrame.this.windowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 100.0;
        gridBagConstraints.weighty = 100.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(dataWanderMainPanel, gridBagConstraints);

        jMenuFile.setText(I18n.getString("filemenu"));

        jMenuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemExit.setText(I18n.getString("exit"));
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemExit);

        jMenuBarMain.add(jMenuFile);

        jMenuEdit.setText(I18n.getString("editmenu"));

        jMenuItemPreferences.setText(I18n.getString("preferences"));
        jMenuItemPreferences.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemPreferencesActionPerformed(evt);
            }
        });
        jMenuEdit.add(jMenuItemPreferences);

        jMenuBarMain.add(jMenuEdit);

        jMenuMigration.setText(I18n.getString("migrationsmenu"));

        jMenuItemRelationalToObject.setText(I18n.getString("relational2object"));
        jMenuItemRelationalToObject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemRelationalToObjectActionPerformed(evt);
            }
        });
        jMenuMigration.add(jMenuItemRelationalToObject);

        jMenuItemObjectToRelational.setText(I18n.getString("object2relational"));
        jMenuMigration.add(jMenuItemObjectToRelational);

        jMenuBarMain.add(jMenuMigration);

        jMenuHelp.setText(I18n.getString("helpmenu"));

        jMenuItemHelp.setText(I18n.getString("helpmenu"));
        jMenuItemHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemHelpActionPerformed(evt);
            }
        });
        jMenuHelp.add(jMenuItemHelp);

        jMenuItemAboutProgram.setText(I18n.getString("aboutprogram"));
        jMenuItemAboutProgram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAboutProgramActionPerformed(evt);
            }
        });
        jMenuHelp.add(jMenuItemAboutProgram);

        jMenuBarMain.add(jMenuHelp);

        setJMenuBar(jMenuBarMain);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemPreferencesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemPreferencesActionPerformed
        PreferenceJFrame frame = getPreferenceJFrame();
        frame.setVisible(true);
    }//GEN-LAST:event_jMenuItemPreferencesActionPerformed

    private void jMenuItemHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemHelpActionPerformed
        JOptionPane.showMessageDialog(this, I18n.getString("helpdescription"),
                I18n.getString("helpmenu"), 
                JOptionPane.INFORMATION_MESSAGE | JOptionPane.OK_OPTION);
    }//GEN-LAST:event_jMenuItemHelpActionPerformed

    private void jMenuItemAboutProgramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAboutProgramActionPerformed
        JOptionPane.showMessageDialog(this, I18n.getString("aboutdescription"),
                I18n.getString("aboutprogram"),
                JOptionPane.INFORMATION_MESSAGE | JOptionPane.OK_OPTION);
    }//GEN-LAST:event_jMenuItemAboutProgramActionPerformed

    private void jMenuItemRelationalToObjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemRelationalToObjectActionPerformed
        dataWanderMainPanel.relationalToObjectMigration();
    }//GEN-LAST:event_jMenuItemRelationalToObjectActionPerformed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        logger.trace("windowClosing()");
        tryClosseApplication();
    }//GEN-LAST:event_windowClosing

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        logger.trace("jMenuItemExitActionPerformed()");
        tryClosseApplication();
    }//GEN-LAST:event_jMenuItemExitActionPerformed

    private void tryClosseApplication() {
        logger.trace("tryClosseApplication()");

        if(dataWanderControler.isMigrationInProgress()) {
            JOptionPane.showMessageDialog(this,
                    I18n.getString("migrationisinpreggrescannotcloseapp"),
                    I18n.getString("errortitle"),
                    JOptionPane.ERROR_MESSAGE | JOptionPane.OK_OPTION);
            return;
        }

        logger.info(I18n.getString("closeapplication"));

        dispose();  // cloase application
        System.exit(0);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DataWanderMainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.googlecode.datawander.View.DataWanderMainPanel dataWanderMainPanel;
    private javax.swing.JMenuBar jMenuBarMain;
    private javax.swing.JMenu jMenuEdit;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenuItem jMenuItemAboutProgram;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemHelp;
    private javax.swing.JMenuItem jMenuItemObjectToRelational;
    private javax.swing.JMenuItem jMenuItemPreferences;
    private javax.swing.JMenuItem jMenuItemRelationalToObject;
    private javax.swing.JMenu jMenuMigration;
    // End of variables declaration//GEN-END:variables

    public void showMigrationProggres(int procente) {
        dataWanderMainPanel.showMigrationProgress(procente);
    }

    public void enterLog(String message) {
        dataWanderMainPanel.enterLog(message);
    }

    public void showSucessMessage() {
        JOptionPane.showMessageDialog(rootPane, I18n.getString("migrationsucessfull"),
                I18n.getString("migrationsucessfullwindowtitle"),
                JOptionPane.INFORMATION_MESSAGE);
    }

    public void showErrorMessage() {
        JOptionPane.showMessageDialog(rootPane, I18n.getString("migrationfailed"),
                I18n.getString("migrationfailedwindowtitle"),
                JOptionPane.ERROR_MESSAGE);
    }

    public void enableInputsControls() {
        dataWanderMainPanel.enableInputsControls();
        enableMenuMigrationsOptions();
    }

    public void disableInputsControls() {
        dataWanderMainPanel.disableInputsControls();
        diableMenuMigrationsOptions();
    }

    private void diableMenuMigrationsOptions() {
        jMenuMigration.setEnabled(false);
        //jMenuItemObjectToRelational.setEnabled(false);
        jMenuItemRelationalToObject.setEnabled(false);
    }

    private void enableMenuMigrationsOptions() {
        jMenuMigration.setEnabled(true);
        //jMenuItemObjectToRelational.setEnabled(true);
        jMenuItemRelationalToObject.setEnabled(true);
    }

    private PreferenceJFrame getPreferenceJFrame() {
        if(preferenceJFrame == null) {
            try {
                preferenceJFrame = new PreferenceJFrame();
            } catch(PreferencesException e) {
                JOptionPane.showMessageDialog(this,
                        I18n.getString("cannotloadpreferencespropertiesfile"),
                        I18n.getString("errortitle"),
                        JOptionPane.ERROR_MESSAGE | JOptionPane.OK_OPTION);
            }
        }
        return preferenceJFrame;
    }

}
